﻿
Partial Class store_Set
    Inherits CProductPage


#Region "Menu Locations"
    Protected Overrides ReadOnly Property TopPanel As System.Web.UI.WebControls.PlaceHolder
        Get
            Return plhTopLinks
        End Get
    End Property
    Protected Overrides ReadOnly Property SubPanel As System.Web.UI.WebControls.PlaceHolder
        Get
            Return plhSubLinks
        End Get
    End Property
    Protected Overrides ReadOnly Property SetPanel1 As System.Web.UI.Control
        Get
            Return plhSetMenu
        End Get
    End Property
    Protected Overrides ReadOnly Property SetPanel2 As System.Web.UI.Control
        Get
            Return plhSetMenu2
        End Get
    End Property
    Protected Overrides ReadOnly Property BottomLinks As System.Web.UI.HtmlControls.HtmlTableRow
        Get
            Return rowLinks
        End Get
    End Property
    Protected Overrides ReadOnly Property BottomRandom As System.Web.UI.HtmlControls.HtmlTableRow
        Get
            Return rowRandom
        End Get
    End Property
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        ClientScript.RegisterStartupScript(Me.GetType, Guid.NewGuid().ToString(), "setCookies()", True)

        'deleted
        If IsNothing(Product) Then
            Response.RedirectPermanent(CSitemap.ConferenceSets)
            Exit Sub
        End If

        'inactive
        If Not Product.ProductIsActive Then
            Response.Redirect(CSitemap.ConferenceSets)
            Exit Sub
        End If

        'old url
        With Request.FilePath.ToLower
            If .Contains("conference_sets/") Then Response.RedirectPermanent(CSitemap.Decide(Product))
        End With

        'Name doesnt match
        If ProductName <> HttpUtility.UrlDecode(CConfig.SafeFriendlyName(Product.ProductName)) AndAlso Request.Url.Host <> "localhost" Then
            Response.RedirectPermanent(CSitemap.Decide(Product))
            Exit Sub
        End If

        lnkDonate.NavigateUrl = CSitemap.External_Donations
        If SpeakerId = ESpeaker.ShaneWillard OrElse Product.ProductSpeakerId = ESpeaker.ShaneWillard Then
            lnkDonate.NavigateUrl = CConfig.External_ShaneWillardDonate
        End If

    End Sub
    Protected Overrides Sub PagePreRender()
        plhSpacing.Visible = Product.Children.Active.Count < 10

        'Set navigation: all sets
        Dim sets As CProductList = CProduct.Cache.Active.ConferenceSets
        NavLinks(lblNavSpeaker, sets, Product, "Collections", CSitemap.ConferenceSets)

        With Product
            'Set navigation: by language
            If .IsEnglish Then
                plhLang.Visible = False
            Else
                NavLinks(lblNavLanguage, sets.GetByLanguageId(.ProductLanguageId), Product, .Language.NativeAndName, CSitemap.ConferenceSets(Nothing, 0, .ProductLanguageId))
            End If

            'Metadata
            Page.Title = .ProductName
            Page.MetaDescription = .ProductDescriptionOrParent
            Page.MetaKeywords = .ProductKeywordsOrParent
            If Page.MetaDescription.Length = 0 Then Page.MetaDescription = CUtilities.Truncate(.Speaker.SpeakerAbout, 400)
            If Not .IsEnglish Then
                Page.Title = String.Concat(.ProductName, " ", .ProductNameChineseSimplified)
                Page.MetaDescription = String.Concat(Page.MetaDescription, vbCrLf, .ProductDescriptionChineseSimplifiedOrParent)
                Page.MetaKeywords = String.Concat(Page.MetaKeywords, vbCrLf, .ProductKeywordsChineseSimplifiedOrParent)
            End If

            'Set Image
            imgProduct.AlternateText = .ProductName
            imgProduct.ToolTip = .ProductName
            imgProduct.ImageUrl = CSitemap.ProductImage(Product)
            imgFb.Attributes("href") = "http://mikeconnellministries.com" & CSitemap.ProductThumb(Product).Substring(1)
            If String.IsNullOrEmpty(.ProductImage) Then
                imgProduct.ImageUrl = CSitemap.SpeakerUploads & .Speaker.SpeakerImage
                imgFb.Attributes("href") = "http://mikeconnellministries.com" & imgProduct.ImageUrl.Substring(1)
            End If

            'Title/Desc
            litTitle.Text = HttpUtility.HtmlEncode(.ProductName)
            If Not .IsEnglish Then
                litNameChineseSimplified.Text = .ProductNameChineseSimplified
            Else
                litNameChineseSimplified.Visible = False
            End If

            'Description, or fail over to individual descriptions (or to info on the speaker)
            lblDescription.Text = .Description_Br
            If String.IsNullOrEmpty(lblDescription.Text) Then
                If Not String.IsNullOrEmpty(.Speaker.SpeakerAbout) AndAlso .ProductSpeakerId <> ESpeaker.MikeConnell Then
                    lblDescription.Text = .Speaker.SpeakerAbout.Replace(vbCrLf, "<br/>")
                End If
            End If

            'Shows the multi-lingual description, if it has one
            If Not .IsEnglish AndAlso Not String.IsNullOrEmpty(.DescriptionChineseSimp_) Then
                lblDescriptionAlt.Text = .DescriptionChineseSimp_Br
            Else
                lblDescriptionAlt.Visible = False
            End If
            plhDescription.Visible = Not String.IsNullOrEmpty(lblDescription.Text) OrElse Not String.IsNullOrEmpty(lblDescriptionAlt.Text)

            'Keywords
            litKeywords.Text = .ProductKeywordsOrParent
            litKeywordsAlt.Text = .ProductKeywordsChineseSimplifiedOrParent
            divKeywords.Visible = .ProductKeywordsOrParent.Trim.Length > 0 OrElse Not String.IsNullOrEmpty(.ProductKeywordsChineseSimplifiedOrParent)

            'Main Links
            For Each i As CProduct In .Children.Active.Ordered
                UCContent(plhContents).Display(i)
            Next


            'Speaker Links (below image)
            'lnkSpeaker.Text = CUtilities.NameAndCount(.SpeakerName, .Speaker.Products.Active.Sermons, "sermon")
            'lnkSpeaker.NavigateUrl = CSitemap.Sermons(Nothing, .ProductSpeakerId)
            'lnkSets.Visible = .Speaker.Products.Active.ConferenceSets.Count > 0
            'lnkSets.Text = CUtilities.NameAndCount(.SpeakerName, .Speaker.Products.Active.ConferenceSets, "set")
            'lnkSets.NavigateUrl = CSitemap.ConferenceSets(Nothing, .ProductSpeakerId)
            'lnkLanguage.Visible = Not .IsEnglish
            'lnkLanguage.Text = CUtilities.NameAndCount(.Language.NativeAndName, CProduct.Cache.Active.Sermons.GetByLanguageId(.ProductLanguageId), "sermon")
            'lnkLanguage.NavigateUrl = CSitemap.Sermons(Nothing, 0, .ProductLanguageId)
            If Not String.IsNullOrEmpty(.Speaker.SpeakerWebsite) Then
                lnkWebsite.Visible = True
                lnkWebsite.Text = .Speaker.SpeakerWebsite
                lnkWebsite.NavigateUrl = "http://" & lnkWebsite.Text
            End If
            If Not String.IsNullOrEmpty(.Speaker.SpeakerContactPhone) Then
                lnkOnAmazon.Visible = True
                lnkOnAmazon.Text = .SpeakerName & " on Amazon..."
                lnkOnAmazon.NavigateUrl = .Speaker.SpeakerContactPhone 'Now used for amazon link
            End If
            If .ProductSpeaker2Id > 0 Then
                lnkWebsite.Visible = True
                lnkWebsite.Text = CUtilities.NameAndCount(.Speaker2Name, .Speaker2.Products.Active, "sermon")
                lnkWebsite.NavigateUrl = CSitemap.Sermons(Nothing, .ProductSpeaker2Id)
                lnkWebsite.Target = ""
            End If


            'Iframe
            ctrlDescriptionLinks.Display(Product)
            ctrlDownloadLinks.Display(Product)

            Dim p As CProduct = Product
            If p.HasParent Then p = p.BelongsTo
            With p
                If .Files.HasMobi OrElse .Files.HasEpub Then
                    UCDownload(plh).Display("eBooks")
                    For Each i As CFile In .Files.Epubs
                        UCDownload(plh).Display(i)
                    Next
                    For Each i As CFile In .Files.Mobis
                        UCDownload(plh).Display(i)
                    Next
                    For Each i As CFile In .Files.PdfsOrWords
                        If i.Product.HasParent Then Continue For
                        UCDownload(plh).Display(i) 'e.g. smashwords pdf ebook
                    Next
                    If Not String.IsNullOrEmpty(.ProductAmazonId) Then
                        UCDownload(plh).Display(CSitemap.External_Amazon(.ProductAmazonId), "Kindle via Amazon", " is also available")
                    End If
                End If

                If .Files.HasPdfOrWord OrElse .HasBook Then
                    UCDownload(plh).Display("Books")
                    For Each i As CFile In .Files.PdfsOrWords
                        If Not i.Product.HasParent Then Continue For
                        UCDownload(plh).Display(i) 'e.g. Manual
                    Next
                    If .HasBook Then
                        If .HasBook2 OrElse .HasBook3 Then
                            If .HasBook Then UCDownload(plh).Display(CSitemap.External_CreateSpace(.ProductCSBookIdOrParent), "Printed Book(1)", " is also available (direct)")
                            If .HasBook2 Then UCDownload(plh).Display(CSitemap.External_CreateSpace(.ProductCSBookId2OrParent), "Printed Book(2)", " is also available (direct)")
                            If .HasBook3 Then UCDownload(plh).Display(CSitemap.External_CreateSpace(.ProductCSBookId3OrParent), "Printed Book(3)", " is also available (direct)")
                        Else
                            UCDownload(plh).Display(CSitemap.External_CreateSpace(.ProductCSBookIdOrParent), "Printed Book", " is also available (direct)")
                        End If
                        Dim bb As CProductList = CProduct.Cache.Paperbacks.GetByCSBookId(p.ProductCSBookId)
                        If bb.Count > 0 Then
                            p = bb(0)
                            If Not String.IsNullOrEmpty(p.ProductAmazonId) Then
                                UCDownload(plh).Display(CSitemap.External_Amazon(.ProductAmazonId), "Printed Book", " is also available (via Amazon)")
                            End If
                        End If
                        bb = CProduct.Cache.Paperbacks.GetByCSBook2Id(p.ProductCSBook2Id)
                        If bb.Count > 0 Then
                            p = bb(0)
                            If Not String.IsNullOrEmpty(p.ProductAmazonId) Then
                                UCDownload(plh).Display(CSitemap.External_Amazon(.ProductAmazonId), "Printed Book", " is also available (via Amazon)")
                            End If
                        End If
                    End If
                End If

                If .Files.HasMp3 Then
                    UCDownload(plh).Display("Audio", "*Save to disk before playing (right-click, save-as)")
                    For Each i As CFile In .Files.Mp3s
                        UCDownload(plh).Display(i)
                    Next
                End If

                If .HasVimeoChannel OrElse .Children.HavingOnVimeo.Count > 0 Then
                    UCDownload(plh).Display("Vimeo")
                    If .HasVimeoChannel Then UCDownload(plh).Display(p, True)
                    For Each i As CProduct In .Children
                        If i.HasVimeo Then UCDownload(plh).Display(i, True)
                    Next
                End If

                If .HasYouTubePlaylist OrElse .Children.HavingOnYouTube.Count > 0 Then
                    UCDownload(plh).Display("YouTube")
                    If .HasYouTubePlaylist Then UCDownload(plh).Display(p, False)
                    For Each i As CProduct In .Children
                        If i.HasYouTube Then UCDownload(plh).Display(i, False)
                    Next
                End If

                If .Files.HasHtm AndAlso Not .Files.HasPdfOrWord Then
                    UCDownload(plh).Display("Notes")
                    For Each i As CFile In .Files.Htms
                        UCDownload(plh).Display(i)
                    Next
                End If
            End With

        End With
    End Sub
#End Region

#Region "User Controls"
    Private Shared Function UCContent(ByVal target As Control) As UCContent
        Dim ctrl As Control = target.Page.LoadControl(CSitemap.Store_UCContent)
        target.Controls.Add(ctrl)
        Return CType(ctrl, UCContent)
    End Function
    Private Shared Function UCDownload(ByVal target As Control) As UCDownload
        Dim ctrl As Control = target.Page.LoadControl(CSitemap.Store_UCDownload)
        target.Controls.Add(ctrl)
        Return CType(ctrl, UCDownload)
    End Function
#End Region

End Class

